package ualrcdg.files;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import android.content.Context;

import com.example.cdg.R;

/**
 * This class creates a game engine that will
 * run the games on the CDG Client and provide
 * interclass operations.
 */
public class GameEngine
{
	public String filename = "";
	//GameStack gamestack = new GameStack();
	//GameLogger gamelogger;
	//RuleResolver ruleresolver = new RuleResolver();
	MediaResolver mediaresolver = new MediaResolver();

	/**
	 * This method creates a file so the game
	 * logger can log the game's events.
	 * @param userId to be used in conjunction with gameId to make the file
	 * @param gameId to be used in conjunction with userId to make the file
	 *
	public void createFile(String userId, String gameId)
	{
		this.filename = userId + gameId + "Game.txt";
		try
		{
			gamelogger = new GameLogger(this.filename);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * This method starts the game for the user.
	 * @param userId to be used in conjunction with gameId to make the file if not already created
	 * @param gameId to be used in conjunction with userId to make the file if not already created
	 *
	public void StartGame(String userId, String gameId) throws IOException
	{
		boolean gameOver = false;
		
		String event = " ";
		String state = " ";
		String rule = " ";
		int statenumber = 0;
		
		while(gameOver == false)
		{
			if((gamelogger.getFirstLine() == null) || (gamelogger.getFirstLine() == "false"))
			{
				gamelogger.logEvent("true");
			}
			else if(gamelogger.getFirstLine() == "true")
			{
				while(gamelogger.getFirstLine() != null)
				{
					gamelogger.getFirstLine();
				}
				while(ruleresolver.hasObjectives())
				{
					if(ruleresolver.checkRule(rule) == true)
					{
						//mediaresolver.deliverContent();
						gamestack.pushState(state);
						gamelogger.logEvent(event + " " + state + statenumber);
						statenumber++;
					}
					ruleresolver.getRule();
				}
				gameOver = true;
			}
			else
			{
				createFile(userId, gameId);
			}
		}
	}
	
	/**
	 * This method retrieves the user's id so
	 * CDG game files will be tied to a user.
	 * @return userId
	 *
	private String getUserId()
	{
		//TODO get user id from account
		return null;
	}
	
	/**
	 * This method retrieves the game's id so
	 * the appropriate game files will be created
	 * and retrieved for the selected game.
	 * @return gameId
	 *
	private String getGameId()
	{
		//TODO get game id from app
		return null;
	}*/
	public final static byte[] getXML(Context context)
	  {
		byte readBuf[] = new byte[512*1024];
	    try { 
	      InputStream istream = context.getResources().openRawResource(R.raw.example);
	      ByteArrayOutputStream bout = new ByteArrayOutputStream();
	      int readCnt = istream.read(readBuf);
	      while (0 < readCnt){
	    	  bout.write(readBuf, 0, readCnt);
	    	  readCnt = istream.read(readBuf);
	      }
	      
	      istream.close();
	      return bout.toByteArray();
	      
	    }
	    catch (Exception e) {
	     
	      return new byte[0];
	    }
	  }
}

